/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame.game;

import java.io.IOException;
import cardgame.network.*;

/**
 *
 * @author Carl
 */
public class ManaPool extends Cards{

    public ManaPool(){
        super(CardPosition.Zone.MANA);
    }
    private ManaAmount totalAmount = new ManaAmount();
    private ManaAmount availableAmount = new ManaAmount();

    @Override
    public void clear(){
        super.clear();
        update();
    }

    @Override
    public void add(Card card){
        super.add(card);
        update();
    }

    @Override
    public void add(Card card, int index){
        super.add(card, index);
        update();
    }

    @Override
    public Card remove(int index){
        Card card = super.remove(index);
        update();
        return card;
    }

    @Override
    public boolean remove(Card card) {
        boolean success = super.remove(card);
        update();
        return success;
    }
    
    public void provide(ManaProvider manaProvider){
        manaProvider.provide();
        update();
    }
    
    public void update(){
        totalAmount.clear();
        availableAmount.clear();
        for(int i=0;i<size();i++){
            Card card = get(i);
            if(card instanceof  ManaProvider){
                ManaProvider manaProvider = (ManaProvider) card;
                totalAmount.add(manaProvider.getProvidedMana());
                if(manaProvider.isAvailable()){
                    availableAmount.add(manaProvider.getProvidedMana());
                }
            }
        }
    }
    
    public boolean isAvailable(ManaAmount manaAmount){
        return availableAmount.contains(manaAmount);
    }

    public ManaAmount getTotalAmount(){
        return totalAmount;
    }

    public ManaAmount getAvailableAmount(){
        return availableAmount;
    }

    @Override
    public void write(BitOutputStream outputStream){
        super.write(outputStream);
        availableAmount.write(outputStream);
        totalAmount.write(outputStream);
    }

    @Override
    public void read(BitInputStream inputStream) throws IOException{
        super.read(inputStream);
        availableAmount.read(inputStream);
        totalAmount.read(inputStream);
    }
}
